package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.CurrentUser;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.po.Product;
import com.zhijian.medical.entity.vo.request.ProductSearchVo;
import com.zhijian.medical.entity.vo.request.ProductSendOutDetailBatchSearchVo;
import com.zhijian.medical.entity.vo.request.ProductSendOutDetailSearchVo;
import com.zhijian.medical.entity.vo.request.ProductSendOutVo;
import com.zhijian.medical.entity.vo.response.MonitorModuleTraceVo;
import com.zhijian.medical.entity.vo.response.ProductSendOutDetailBatchVo;
import com.zhijian.medical.entity.vo.response.ProductSendOutDetailVo;
import com.zhijian.medical.entity.vo.response.ProductVo;
import com.zhijian.medical.service.MonitorModuleService;
import com.zhijian.medical.service.ProductSendOutDetailBatchService;
import com.zhijian.medical.service.ProductSendOutDetailService;
import com.zhijian.medical.service.ProductService;
import com.zhijian.medical.util.Page;
import com.zhijian.medical.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;

/**
 * 产品表 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-01-17 14:55:39
 */
@Slf4j
@Controller
@Api(tags = "产品表")
public class ProductController {

    @Autowired
    private ProductService service;

    @Autowired
    private ProductSendOutDetailService productSendOutDetailService;

    @Autowired
    private MonitorModuleService monitorModuleService;

    @Autowired
    private ProductSendOutDetailBatchService productSendOutDetailBatchService;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/product")
    @ApiOperation("新增")
    public boolean insert(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody Product param) {
        param.setApplyUserId(personDto.getId());
        param.setApplyUserName(personDto.getName());
        param.setApplyUserGroupIds(StrUtil.join(",", personDto.getGroupIds()));
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != null;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/product/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/product/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody Product param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/product/{id}")
    @ApiOperation("根据id查询")
    public ProductVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/product/page")
    @ApiOperation("分页查询")
    public Page<ProductVo> selectByCondition(@CurrentUser @ApiIgnore PersonDto personDto,
                                             @RequestBody(required = false) ProductSearchVo param) {
        if (param == null)
            param = ProductSearchVo.builder().build();
        param = service.checkRoleParam(personDto, param);
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 分页查询发货详情
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/product/send/out/detail/page")
    @ApiOperation("分页查询发货详情")
    public Page<ProductSendOutDetailVo> selectByCondition(@RequestBody ProductSendOutDetailSearchVo param) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return productSendOutDetailService.selectPage(param);
    }

    /**
     * 根据发货号查询单个发货详情
     *
     * @param code
     * @return
     */
    @Response
    @GetMapping("/product/send/out/detail/{code}")
    @ApiOperation("根据发货号查询单个发货详情")
    public ProductSendOutDetailVo selectProductSendOutDetailVoById(@PathVariable("code") String code) {
        return productSendOutDetailService.selectVoByCode(code);
    }

    /**
     * 审批
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/product/approve/{id}")
    @ApiOperation("审批")
    public boolean approve(@CurrentUser @ApiIgnore PersonDto personDto, @PathVariable("id") Long id) {
        return service.approve(id);
    }

    /**
     * 取消
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/product/cancel/{id}")
    @ApiOperation("取消")
    public boolean cancel(@PathVariable("id") Long id) {
        return service.cancel(id);
    }

    /**
     * 发货
     *
     * @param id
     * @param productSendOutVo
     * @return
     */
    @Response
    @PostMapping("/product/send/out/{id}")
    @ApiOperation("发货")
    public boolean sendOut(@ApiParam(name = "id", value = "产品订单id") @PathVariable("id") Long id,
                           @RequestBody ProductSendOutVo productSendOutVo) {
        productSendOutVo.setProductId(id);
        return service.sendOut(productSendOutVo, false);
    }

    /**
     * 确认收货
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/product/send/out/detail/harvest/{id}")
    @ApiOperation("确认收货")
    public boolean harvest(@CurrentUser @ApiIgnore PersonDto personDto,
                           @ApiParam(name = "id", value = "发货记录id") @PathVariable("id") Long id) {
        return productSendOutDetailService.harvest(Objects.nonNull(personDto) ? personDto.getId() : null, id);
    }

    /**
     * 申请人员自己确认收货
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/product/send/out/detail/me/harvest/{id}")
    @ApiOperation("申请人员自己确认收货")
    public boolean applyUserHarvest(@PathVariable("id") Long id,
                                    @RequestBody ProductSendOutVo productSendOutVo) {
        productSendOutVo.setProductId(id);
        return service.applyUserHarvest(productSendOutVo);
    }

    /**
     * 查询产品订单某个发货记录的轨迹
     *
     * @param productIdSendOutDetailId
     * @return
     */
    @Response
    @PostMapping("/product/detail/trace/{productIdSendOutDetailId}")
    @ApiOperation("查询产品订单某个发货记录的轨迹")
    public List<MonitorModuleTraceVo> selectMonitorModuleTraceVoByProductId(@ApiParam(name = "productIdSendOutDetailId", value = "产品订单发货记录id")
                                                                            @PathVariable("productIdSendOutDetailId") Long productIdSendOutDetailId) {
        return monitorModuleService.selectMonitorModuleTraceVoListByProductSendOutDetailId(productIdSendOutDetailId);
    }

    /**
     * 查询单次发货的具体批次信息
     *
     * @param productIdSendOutDetailId
     * @return
     */
    @Response
    @PostMapping("/product/send/out/detail/batch/{productIdSendOutDetailId}")
    @ApiOperation("查询单次发货的具体批次信息（目的展示二维码）")
    public List<ProductSendOutDetailBatchVo> selectProductSendOutDetailBatchVoByDetailId(@ApiParam(name = "productIdSendOutDetailId", value = "产品订单发货记录id")
                                                                                         @PathVariable("productIdSendOutDetailId") Long productIdSendOutDetailId) {
        return productSendOutDetailBatchService.selectList(ProductSendOutDetailBatchSearchVo.builder()
                .detailId(productIdSendOutDetailId)
                .build());
    }

}